//
//  MessageViewController.m
//  share_business
//
//  Created by 朱永福 on 2017/9/24.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "MessageViewController.h"
#import "MsgDelView.h"
#import "SystemMsgViewCell.h"
#import <Masonry/Masonry.h>

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *dataArray;//数据源
@property (nonatomic ,strong) NSMutableArray *deleteArray;//删除的数据
@property (nonatomic ,assign) BOOL isInsertEdit;//tableview编辑方式的判断
@property (nonatomic ,strong) MsgDelView *bottomView;//底部视图

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.tableView registerClass:[SystemMsgViewCell class] forCellReuseIdentifier:@"cells"];
    [self.view addSubview:self.tableView];
    _isInsertEdit = NO;
    //获取数据
    [self getTableviewData];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationItem.title = @"消息中心";
    [self topLeftItemButton:[UIImage imageNamed:@"back"] title:@""];
    [self topRightItemButton:nil image2:nil title1:@"全部已读" title2:@"编辑"];
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] init];
        self.tableView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
            make.top.equalTo(self.view);
            make.left.right.bottom.equalTo(self.view);
        }];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)deleteArray{
    if (!_deleteArray) {
        self.deleteArray = [NSMutableArray array];
    }
    return _deleteArray;
}

/**
 删除数据方法
 */
- (void)deleteData{
    if (self.deleteArray.count >0) {                             
        [self.dataArray removeObjectsInArray:self.deleteArray];
        [self.tableView reloadData];
    }
    
}

- (void)getTableviewData{
    //拟造数据，此数据需要从服务器获取
    NSArray *array = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
    //添加到数据源中
    [self.dataArray addObjectsFromArray:array];
    [self.tableView reloadData];
}


- (void)tapAllBtn:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    if (btn.selected) {
        
        for (int i = 0; i< self.dataArray.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            //全选实现方法
            [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
        }
        
        //点击全选的时候需要清除deleteArray里面的数据，防止deleteArray里面的数据和列表数据不一致
        if (self.deleteArray.count >0) {
            [self.deleteArray removeAllObjects];
        }
        [self.deleteArray addObjectsFromArray:self.dataArray];
        
        [btn setTitle:@"取消" forState:UIControlStateNormal];
        
    }else{
        
        //取消选中
        for (int i = 0; i< self.dataArray.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            [_tableView deselectRowAtIndexPath:indexPath animated:NO];
            
        }
        
        [btn setTitle:@"全选" forState:UIControlStateNormal];
        [self.deleteArray removeAllObjects];
    }
    
    
    //    NSLog(@"+++++%ld",self.deleteArray.count);
    //    NSLog(@"===%@",self.deleteArray);
    
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SystemMsgViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cells" forIndexPath:indexPath];

    
      cell.label.text = self.dataArray[indexPath.row];
    
    //处理选中背景色问题
    UIView *backGroundView = [[UIView alloc]init];
    backGroundView.backgroundColor = [UIColor clearColor];
    cell.selectedBackgroundView = backGroundView;
    
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //根据不同状态返回不同编辑模式
    if (_isInsertEdit) {
        
        return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
        
    }else{
        
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
    
    //左滑删除数据方法
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataArray removeObjectAtIndex: indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]
                              withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView reloadData];
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //正常状态下，点击cell进入跳转下一页
    //在编辑模式下点击cell 是选中数据
    if (self.getRightButton2.selected) {
        NSLog(@"选中");
        [self.deleteArray addObject:[self.dataArray objectAtIndex:indexPath.row]];
        
    }else{
        NSLog(@"跳转下一页");
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    if (self.getRightButton2.selected) {
        NSLog(@"撤销");
        [self.deleteArray removeObject:[self.dataArray objectAtIndex:indexPath.row]];
        
    }else{
        NSLog(@"取消跳转");
    }
    
    
}

//左边按钮
-(void)defaultButtonAction:(UIButton *)button{
    NSLog(@"leftAction");
    if ([button isSelected]) {
        [[self getRightButton2] setTitle:@"编辑" forState:UIControlStateNormal];
        
        _isInsertEdit = NO;
        [_tableView setEditing:NO animated:YES];
        
        [[self getLeftButton] setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [[self getLeftButton] setTitle:nil forState:UIControlStateNormal];

        [[self getLeftButton] setSelected:NO];

    }else{
        UINavigationController *nav = self.navigationController;
        //    if (nav.viewControllers.count == 1) {
        //        [self dismissViewControllerAnimated:YES completion:nil];
        //    }
        [nav popViewControllerAnimated:YES];
    }
   
}
//右边第一个按钮
-(void)right1Action:(UIButton *)button{
    NSLog(@"right1Action");
}

//右边第二个按钮
-(void)right2Action:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        //点击编辑的时候清空删除数组
        [self.deleteArray removeAllObjects];
        [sender setTitle:@"全选" forState:UIControlStateNormal];
        _isInsertEdit = YES;//这个时候是全选模式
        [_tableView setEditing:YES animated:YES];
        
        [[self getLeftButton] setTitle:@"取消" forState:UIControlStateNormal];
        [[self getLeftButton] setImage:nil forState:UIControlStateNormal];
        
        [[self getLeftButton] setSelected:YES];
        
        //        //如果在全选状态下，点击完成，再次进来的时候需要改变按钮的文字和点击状态
        //        if (_bottomView.allBtn.selected) {
        //            _bottom_view.allBtn.selected = !_bottom_view.allBtn.selected;
        //            [_bottom_view.allBtn setTitle:@"全选" forState:UIControlStateNormal];
        //        }
        //
        //        //添加底部视图
        //        CGRect frame = self.bottom_view.frame;
        //        frame.origin.y -= 50;
        //        [UIView animateWithDuration:0.5 animations:^{
        //            self.bottom_view.frame = frame;
        //            [self.view addSubview:self.bottom_view];
        //        }];
        
        
        
    }
    //    else{
    //        [sender setTitle:@"编辑" forState:UIControlStateNormal];
    //        _isInsertEdit = NO;
    //        [_tableView setEditing:NO animated:YES];
    
    //        [UIView animateWithDuration:0.5 animations:^{
    //            CGPoint point = self.bottom_view.center;
    //            point.y      += 50;
    //            self.bottom_view.center   = point;
    //            
    //        } completion:^(BOOL finished) {
    //            [self.bottom_view removeFromSuperview];
    //        }];
    //    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
